package logic

import (
	"context"
	"errors"
	"house_srv/mysql"

	"house_srv/house"
	"house_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type HouseReleaseLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHouseReleaseLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HouseReleaseLogic {
	return &HouseReleaseLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *HouseReleaseLogic) HouseRelease(in *house.HouseReleaseRequest) (*house.HouseReleaseResponse, error) {
	// todo: add your logic here and delete this line
	h := mysql.House{
		UserId:    in.UserId,
		HouseName: in.HouseName,
		Price:     in.Price,
		Floor:     in.Floor,
		Area:      in.Area,
		Cover:     in.Cover,
		Address:   in.Address,
		Describe:  in.Describe,
	}
	err := h.HouseInfo(int64(h.ID))
	if err != nil {
		logx.Error("获取房源信息不存在")
		return nil, errors.New("获取房源信息不存在")
	}
	err = h.Create()
	if err != nil {
		logx.Error("创建房源信息失败")
		return nil, errors.New("创建房源信息失败")
	}
	return &house.HouseReleaseResponse{
		HouseId: int64(h.ID),
	}, nil
}
